<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <treeTools :tree-node="company" :is-root="true" @addDepartment="addDepartment" />
        <hr>
        <el-tree :data="depts" :props="defaultProps" default-expand-all>
          <template slot-scope="row">
            <treeTools :tree-node="row.data" @delDepartment="getDepartment" @addDepartment="addDepartment" @editDepartment="editDepartment" />
          </template>
        </el-tree>
      </el-card>
      <addDepartDialog ref="addDepartDialog" :tree-node="currentTreeNode" @addDepartmentSuccess="getDepartment" />
    </div>
  </div>
</template>

<script>
import treeTools from './component/tree-tools.vue'
import { getDepartmentList } from '@/api/department'
import { toTreeData } from '@/utils/index'
import addDepartDialog from './component/addDepartDialog.vue'
export default {
  components: { treeTools, addDepartDialog },
  data() {
    return {
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人', id: '' },
      defaultProps: {
        label: 'name'
      },
      depts: [],
      currentTreeNode: {}
    }
  },
  created() {
    // // 获取组织架构信息
    // const { depts } = await getDepartmentList()
    // // console.log(depts)
    // // 调用封装的递归方法，对数据格式进行转换（转成饿了么需要的树形结构）
    // this.depts = toTreeData(depts, '')

    // 调用获取组织架构的方法
    this.getDepartment()
  },
  methods: {
    // 封装获取组织架构的方法（包括对数据进行加工）
    async getDepartment() { // 获取组织架构信息
      const { depts } = await getDepartmentList()
      // console.log(depts)
      // 调用封装的递归方法，对数据格式进行转换（转成饿了么需要的树形结构）
      this.depts = toTreeData(depts, '')
    },
    // 添加部门
    addDepartment(treeNode) {
      this.currentTreeNode = treeNode
      this.$refs.addDepartDialog.title = '添加部门'
      this.$refs.addDepartDialog.isShowDialog = true
    },
    // 编辑部门
    async editDepartment(treeNode) {
      this.currentTreeNode = treeNode
      await this.$refs.addDepartDialog.getDepartmentDetail(treeNode.id)
      this.$refs.addDepartDialog.title = '编辑部门'
      this.$refs.addDepartDialog.isShowDialog = true
    }
  }
}

</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size:14px;
}
</style>

